﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TFSWitAdmin
{
  class ListItemWorkItem
  {
    public ListItemWorkItem(WorkItem workItem)
    {
      WI = workItem;
    }

    public override string ToString()
    {
      return WI.Title;
    }
    public WorkItem WI;

  }


  internal class Generals
  {

    public static void ExecuteShellCommand(string _FileToExecute, string _CommandLine, ref string _outputMessage,ref string _errorMessage)
  {
    System.Diagnostics.Process _Process = null;
    try
    {
      _FileToExecute = "\"" + _FileToExecute + "\"";
      _Process = new System.Diagnostics.Process();
      string _CMDProcess = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\cmd.exe",
                                         new object[] {Environment.SystemDirectory});
      string _Arguments = string.Format(System.Globalization.CultureInfo.InvariantCulture, "/C {0}",
                                        new object[] {_FileToExecute});
      if (!string.IsNullOrEmpty(_CommandLine))
      {
        _Arguments += string.Format(System.Globalization.CultureInfo.InvariantCulture, " {0}",
                                    new object[] {_CommandLine, System.Globalization.CultureInfo.InvariantCulture});
      }
      var _ProcessStartInfo = new System.Diagnostics.ProcessStartInfo(_CMDProcess,
                                                                                                      _Arguments);
      _ProcessStartInfo.CreateNoWindow = true;
      _ProcessStartInfo.UseShellExecute = false;
      _ProcessStartInfo.RedirectStandardOutput = true;
      _ProcessStartInfo.RedirectStandardInput = true;
      _ProcessStartInfo.RedirectStandardError = true;
      _Process.StartInfo = _ProcessStartInfo;
      _Process.Start();
      _errorMessage = _Process.StandardError.ReadToEnd();
      _Process.WaitForExit();
      _outputMessage = _Process.StandardOutput.ReadToEnd();
      _Process.WaitForExit();
    }
    catch (Win32Exception _Win32Exception)
    {
      Console.WriteLine("Win32 Exception caught in process: {0}", _Win32Exception.ToString());
    }
    catch (Exception _Exception)
    {
      Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
    }
    finally
    {
      if (_Process != null)
      {
        _Process.Close();
        _Process.Dispose();
      }
      _Process = null;
    }
  }
  }
}
